﻿using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using log4net.Config;
using MsgSender.AppSetting;

namespace MsgSender
{
    public class AppManager
    {
        /// <summary>
        /// 全局唯一的appManager
        /// </summary>
        public static AppManager appManager = null;

        #region 属性
        /// <summary>
        /// 待处理的窗口
        /// </summary>
        public List<CWindow.CWindow> windows { get; set; }

        /// <summary>
        /// logApp
        /// </summary>
        public ILog logApp { get { return _log; } }
        #endregion
       

        private ILog _log=null;

       
      


        /// <summary>
        /// 全局appManager
        /// </summary>
        private AppManager()
        {
            ConfigLog4Net();
            _log = LogManager.GetLogger(typeof(AppManager));
            appManager = this;
            windows = new List<CWindow.CWindow>();
        }

        /// <summary>
        /// 通过配置初始化log4net
        /// </summary>
        public void ConfigLog4Net() {
            string cfg = System.Configuration.ConfigurationManager.AppSettings["log4net"];
            System.IO.FileInfo cfgFileInfo = new System.IO.FileInfo(cfg);
            if (!cfgFileInfo.Exists) return;
            XmlConfigurator.Configure(cfgFileInfo);
        }

        /// <summary>
        /// 获取唯一的appManager
        /// </summary>
        /// <returns></returns>
        public static AppManager GetAppManager()
        {
            if (appManager == null)
                appManager = new AppManager();

            return appManager;     
        }

        /// <summary>
        /// 获取匹配窗口
        /// </summary>
        public void GetMatchWindows()
        {
            windows.Clear();
            if (!win32API.win32API.EnumDesktopWindows(IntPtr.Zero, new win32API.win32API.EnumDesktopWindowsDelegate(EnumWindowsProc), IntPtr.Zero))
            {
                appManager.logApp.Error("遍历所有窗口时出错:" + win32API.win32API.GetLastError().ToString());
            }

        }

        /// <summary>
        /// 枚举回调函数的代理
        /// </summary>
        /// <param name="hWnd">窗体句柄</param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        private static bool EnumWindowsProc(IntPtr hWnd, uint lParam)
        {
            try
            {
                //不是窗口或者窗口不显示的 直接跳过
                if (!win32API.win32API.IsWindow(hWnd) || !win32API.win32API.IsWindowVisible(hWnd))
                    return true;
                //获取窗口类名
                StringBuilder className = new StringBuilder(256);
                win32API.win32API.GetClassName(hWnd, className, className.Capacity);

                //匹配窗口类型
                if (AppSettings.matchClassName.Contains(className.ToString()))
                {
                    StringBuilder caption = new StringBuilder(256);
                    win32API.win32API.GetWindowText(hWnd, caption, caption.Capacity);
                    if (!AppSettings.filterCaption.Contains(caption.ToString()))
                    {
                        //加入窗口列表
                        appManager.windows.Add(new CWindow.CWindow(hWnd, caption.ToString(), className.ToString()));
                        appManager.logApp.Info(string.Format("捕捉窗口：{0}，标题{1}类名：{2}", hWnd.ToString("X"), caption.ToString(), className.ToString()));
                    }
                }
                return true;
                
            }
            catch (Exception e) {
                appManager.logApp.Error(e);
                return false;
            }
        }
   

    }
}
